package com.google.code.p.narcissus.core.remote;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * @see http://forums.sun.com/thread.jspa?threadID=489302
 * @author Jérôme JADOULLE [JADOJ] - 24 sept. 2010
 */
public class NarcissusServer {

	public static final String NARCISSUS_SERVER_TYPE_KEY = "narcissus.server.type";

	public static final String NARCISSUS_SERVER_HOST_KEY = "narcissus.server.host";

	public static final String NARCISSUS_SERVER_PORT_KEY = "narcissus.server.port";

	public static final String NARCISSUS_SERVICE_NAME_KEY = "narcissus.service.name";

	public static final Logger LOGGER = Logger.getLogger(NarcissusServer.class.getName());

	public static void main(String[] args) throws RemoteException, AlreadyBoundException, InterruptedException {
		DOMConfigurator.configure(NarcissusServer.class.getResource("/log4j-narcissus-core.xml"));
		NarcissusServer.start();
	}

	private static void start() throws RemoteException, AlreadyBoundException, InterruptedException {
		Integer serverPort = Integer.valueOf(System.getProperty("NARCISSUS_SERVER_PORT_KEY", "2004"));
		Registry localreg = LocateRegistry.createRegistry(serverPort);
		RemoteRobot service = new RemoteRobot(serverPort);
		localreg.bind(System.getProperty("NARCISSUS_SERVICE_NAME_KEY", "ScreenShooter"), service);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		LOGGER.info("Narcissus server started on " + format.format(new Date()));
	}
}
